package com.wit.WEB.controller;

import com.wit.WEB.entity.*;
import com.wit.WEB.service.DangerService;
import com.wit.WEB.service.OrderService;
import com.wit.WEB.service.TestStandService;
import com.wit.WEB.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: java-web-group
 * @description:
 * @author: KingTeng
 * @create: 2022-03-11 08:27
 **/
@Controller
public class AdminController {
    private DangerService dangerService;
    private OrderService orderService;
    private TestStandService testStandService;
    private UserService userService;
    @Autowired
    private void setUserService(UserService userService){this.userService=userService;
    }
    @Autowired
    private void setOrderService(OrderService orderService){
        this.orderService=orderService;
    }
    @Autowired
    private void setDangerService(DangerService dangerService){
        this.dangerService=dangerService;
    }
    @Autowired
    private void setTestStandService(TestStandService testStandService){
        this.testStandService=testStandService;
    }

    @RequestMapping("/ForAdmin")
    public String tolist(){

        return "/admin/listUser";
    }

    @RequestMapping(value = { "/getOrderAll" }, method = {RequestMethod.GET }, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List getGoodsAll(){

        List<OrderCount> orderCounts=new ArrayList<>();
        List<Order_> orderList = orderService.findall();

        Integer[] a=new Integer[orderList.size()];
        for(int i=0;i<a.length;i++){
            a[i]=0;
        }
        for(Order_ o:orderList){
            a[Integer.parseInt(o.getAddress())]+=1;
        }

        for(int i=0;i<a.length;i++){
            if (a[i]!=0){
                OrderCount orderCount=new OrderCount();
                orderCount.setAddress(Integer.toString(i));
                orderCount.setUid(a[i]);
                orderCounts.add(orderCount);
            }
        }

        for(OrderCount i:orderCounts){
            System.out.println(i.toString());
        }
        return orderCounts;
    }


    @RequestMapping("/forListDanger")
    public String ListOrder(HttpSession session){
        List<Danger> dangers=dangerService.findAll();
        session.setAttribute("dangers", dangers);
        return "/admin/ManageDanger";
    }

    @RequestMapping("/AddDanger")
    public String AddDanger(String city,String lv,HttpSession session){
        Integer l=0;
        if (lv.equals("中风险")){
            l=2;
        }
        else if (lv.equals("高风险")){
            l=3;
        }
        Danger danger=new Danger();
        danger.setCity(city);
        danger.setLv(l);
        dangerService.AddDanger(danger);
        return "forward:/forListDanger";
    }

    @RequestMapping("/DeleteDanger")
    public String deleted(@RequestParam(value = "city",required = true) String city, HttpSession session){
        dangerService.deleteByCity(city);
        return "forward:/forListDanger";
    }

    @RequestMapping("/DeleteStation")
    public String deletestation(@RequestParam(value = "id",required = true) String id, @RequestParam(value = "uid",required = true) String uid,HttpSession session){
        testStandService.deleteByID(id);
        userService.deleteByID(uid);
        return "forward:/forListStand";
    }

    @RequestMapping("/forListStand")
    public String ListStand(HttpSession session){
        List<TestStand> stands=testStandService.findall();
        session.setAttribute("stands", stands);
        return "/admin/ManageStand";
    }

    @RequestMapping("/toAddStand")
    public String addStand(HttpSession session){
        return "/admin/AddStand";
    }

    @PostMapping("/forAddStand")
    public String register(String account, String pwd,String city,String address,String nam,String phone, Model model) {
        boolean iron=userService.isExist(account);
        if (iron){
            String msg = "用户名已经被使用,不能使用";
            model.addAttribute("msg", msg);
            return "forward:/toAddStand";
        }

        TestStand testStand=new TestStand();
        userService.insertUserStand(account, pwd);
        User user=userService.selectByAcAndPW(account, pwd);

        testStand.setCity(city);
        testStand.setNam(nam);
        testStand.setAddress(address);
        testStand.setPhone(phone);
        testStand.setUid(user.getId());
        testStandService.inser(testStand);
        System.out.println(user.toString());
        System.out.println(testStand.toString());
        return "forward:/forListStand";
    }
}
